package la.foton.atividade.controller;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;
import javax.persistence.PersistenceException;
import la.foton.atividade.util.PropertiesUtil;
import la.foton.atividade.util.exception.TreatedException;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public abstract class AbstractBaseBean implements Serializable, MethodInterceptor
{


   private static final long serialVersionUID = 1L;
   private static final Logger logger = LoggerFactory.getLogger(AbstractBaseBean.class);
   @Autowired
   protected MailSender mailSender;

   protected abstract void addErrorMessage(String componentId, String errorMessage);

   protected abstract void addErrorMessage(String errorMessage);

   protected abstract void addInfoMessage(String componentId, String infoMessage);

   protected abstract void addInfoMessage(String infoMessage);

   protected void addMessage(String componentId, String errorMessage, Severity severity)
   {
      FacesMessage message = new FacesMessage(errorMessage);
      message.setSeverity(severity);
      FacesContext.getCurrentInstance().addMessage(componentId, message);
   }

   public Object invoke(MethodInvocation method) throws Throwable
   {
      Object result = null;
      try
      {
         result = method.proceed();
      }
      catch (IllegalArgumentException ie)
      {
         addErrorMessage(null, PropertiesUtil.MESSAGE_EXCEPTION.getValue(ie.getClass().getName()));
         logger.debug(null, ie);

         throw ie;
      }
      catch (PersistenceException pe)
      {
         addErrorMessage(null, PropertiesUtil.MESSAGE_ERROR.getValue(1));
         logger.debug(null, pe);

         throw pe;
      }
      catch (Exception e)
      {
         // Building stack trace string
         StringWriter stackTrace = new StringWriter();
         e.printStackTrace(new PrintWriter(stackTrace));
         logger.debug(null, e);
         // Building e-mail
         SimpleMailMessage email = new SimpleMailMessage();
         email.setFrom("gleyson.santos@foton.la");

         email.setTo("gleyson.santos@foton.la");
         email.setSubject("[MyApp] Exception in '" + method.getMethod().getName() + "' method");
         email.setText(
                  "Exception in: " + method.getMethod().getName() + "\n\n" +
                     "Class: " + method.getMethod().getDeclaringClass().getName() + "\n\n" +
                     "Message: " + e.getMessage() + "\n\n" +
                     "StackTrace:\n" + stackTrace.getBuffer().toString()
                  );
         // Sending e-mail
         addErrorMessage(null, "teste");
         try
         {
            // this.mailSender.send(email);
         }
         catch (MailException mailException)
         {
            // logger.error(mailException.getMessage());
         }
         throw new TreatedException();

      }
      return result;
   }

}
